package cn.mccraft.uide.api.util;

import java.util.List;
import java.util.Optional;

import com.google.common.collect.Lists;

import cn.mccraft.uide.api.UIDEAPI;
import ro.fortsoft.pf4j.PluginWrapper;

public interface PluginUtils {

	static Optional<PluginWrapper> getPlugin() {
		List<StackTraceElement> stackTrace = Lists.newArrayList(Thread.currentThread().getStackTrace());
		stackTrace.removeIf(ele -> ele.getClassName().startsWith("cn.mccraft.uide.api") || ele.getClassName().startsWith("cn.mccraft.uide.core"));
		try {
			return Optional.ofNullable(UIDEAPI.getPluginManager().whichPlugin(
								Class.forName(stackTrace.get(0).getClassName())));
		} catch (ClassNotFoundException ignored) {
			return Optional.empty();
		}
	}

	static Optional<PluginWrapper> getPlugin(Class<?> clazz) {
		return Optional.ofNullable(UIDEAPI.getPluginManager().whichPlugin(clazz));
	}

}
